package com.puboot.module.blog.controller;

import com.puboot.common.util.*;
import com.puboot.module.admin.vo.base.ResponseVo;
import com.puboot.module.blog.model.*;
import com.puboot.module.blog.service.*;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/blog/aviation/api")
@AllArgsConstructor
public class AviationController {

    private final HttpServletRequest httpServletRequest;
    private final AviationService aviationService;
    private final AviationDetailJService aviationDetailJService;
    private final AviationDetailIService aviationDetailIService;

    @PostMapping("/result")
    @Transactional
    public ResponseVo result(Aviation aviation) {
        try {
            HttpSession httpSession = httpServletRequest.getSession();
            Customer customer = (Customer) httpSession.getAttribute("customer");
            List<String> ncvis = new ArrayList<>();
            if (StringUtils.isNotBlank(aviation.getNcvis())) {
                ncvis = Arrays.asList(aviation.getNcvis().split(","));
            }
            List<String> fcis = new ArrayList<>();
            if (StringUtils.isNotBlank(aviation.getFcis())) {
                fcis = Arrays.asList(aviation.getFcis().split(","));
            }
            List<String> ccis = new ArrayList<>();
            if (StringUtils.isNotBlank(aviation.getCcis())) {
                ccis = Arrays.asList(aviation.getCcis().split(","));
            }
            List<String> ofis = new ArrayList<>();
            if (StringUtils.isNotBlank(aviation.getOfis())) {
                ofis = Arrays.asList(aviation.getOfis().split(","));
            }
            List<String> ncvjs = new ArrayList<>();
            if (StringUtils.isNotBlank(aviation.getNcvjs())) {
                ncvjs = Arrays.asList(aviation.getNcvjs().split(","));
            }
            List<String> fcjs = new ArrayList<>();
            if (StringUtils.isNotBlank(aviation.getFcjs())) {
                fcjs = Arrays.asList(aviation.getFcjs().split(","));
            }
            List<String> ccjs = new ArrayList<>();
            if (StringUtils.isNotBlank(aviation.getCcjs())) {
                ccjs = Arrays.asList(aviation.getCcjs().split(","));
            }
            List<String> ofjs = new ArrayList<>();
            if (StringUtils.isNotBlank(aviation.getOfjs())) {
                ofjs = Arrays.asList(aviation.getOfjs().split(","));
            }
            List<String> bfjs = new ArrayList<>();
            if (StringUtils.isNotBlank(aviation.getBfjs())) {
                bfjs = Arrays.asList(aviation.getBfjs().split(","));
            }

            Double eBurn = AviationUtil.getEBurn(ncvis, fcis, ccis, ofis, ncvjs, fcjs, bfjs, ccjs, ofjs);
            Double ePurchaseElectric = AviationUtil.getEPurchaseElectric(aviation.getAdPurchaseElectric(), aviation.getEfPurchaseElectric());
            Double ePurchaseHeat = AviationUtil.getEPurchaseHeatOrEOutputHeat(aviation.getAdPurchaseHeat());
            Double eOutputHeat = AviationUtil.getEPurchaseHeatOrEOutputHeat(aviation.getAdOutInputHeat());
            Double eOutputElectric = AviationUtil.getEOutputElectric(aviation.getAdOutInputElectric(), aviation.getEfOutInputElectric());
            Double result = AviationUtil.getE(eBurn, ePurchaseElectric, ePurchaseHeat, eOutputElectric, eOutputHeat);
            aviation.setCustomerId(customer.getId());
            aviation.setCreateTime(new Date());
            aviation.setEBurn(GasDCUtil.round(eBurn, 2));
            aviation.setEPurchaseElectric(GasDCUtil.round(ePurchaseElectric, 2));
            aviation.setEPurchaseHeat(GasDCUtil.round(ePurchaseHeat, 2));
            aviation.setEOutputHeat(GasDCUtil.round(eOutputHeat, 2));
            aviation.setEOutputElectric(GasDCUtil.round(eOutputElectric, 2));
            aviation.setState(Constants.STATE_INVAILD);
            aviation.setResult(GasDCUtil.getThousandth(result));
            aviationService.save(aviation);
            for (int i = 0; i < ncvis.size(); i++) {
                AviationDetailI aviationDetailI = new AviationDetailI();
                aviationDetailI.setNcvi(Double.valueOf(ncvis.get(i)));
                aviationDetailI.setFci(Double.valueOf(fcis.get(i)));
                aviationDetailI.setCci(Double.valueOf(ccis.get(i)));
                aviationDetailI.setOfi(Double.valueOf(ofis.get(i)));
                aviationDetailI.setAviationId(aviation.getId());
                aviationDetailIService.save(aviationDetailI);
            }
            for (int i = 0; i < ncvjs.size(); i++) {
                AviationDetailJ aviationDetailJ = new AviationDetailJ();
                aviationDetailJ.setNcvj(Double.valueOf(ncvjs.get(i)));
                aviationDetailJ.setFcj(Double.valueOf(fcjs.get(i)));
                aviationDetailJ.setCcj(Double.valueOf(ccjs.get(i)));
                aviationDetailJ.setOfj(Double.valueOf(ofjs.get(i)));
                aviationDetailJ.setBfj(Double.valueOf(bfjs.get(i)));
                aviationDetailJ.setAviationId(aviation.getId());
                aviationDetailJService.save(aviationDetailJ);
            }
            return ResultUtil.success("计算成功", aviation);
        } catch (Exception e) {
            log.error("计算失败", e);
            return ResultUtil.error("计算失败");
        }
    }


}
